﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SXICE.Core.Models;
using SXICE.IService;
using SXICE.IService.Models;
using SXICE.WebAPI.Utilities;
using AutoMapper;
using SXICE.Core.Domains;

namespace SXICE.WebAPI.Controllers
{
    /// <summary>
    /// 其他控制器
    /// </summary>
    [Route("")]
    [Authorize]
    public class OtherController : ControllerBase
    {
        private readonly IOtherService otherService;

        public OtherController(IOtherService otherService)
        {
            this.otherService = otherService;
        }

        /// <summary>
        /// 获取最新更新信息
        /// </summary>
        /// <returns>结果</returns>
        [HttpGet]
        [AllowAnonymous]
        public ReturnResult<UpdateInfo> GetLastedUpdateInfo()
        {
            return otherService.GetClientUpdateInfo();
        }

        /// <summary>
        /// 获取最新更新的文件
        /// </summary>
        /// <param name="mainVersion">主版本</param>
        /// <returns>结果</returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult GetUpdateFile(int mainVersion)
        {
            var result = otherService.GetClientUpdateFile(mainVersion);
            if (!result.Success)
                return new JsonResult(result);
            return File(System.IO.File.OpenRead(result.Result), "application/x-msdownload");
        }
    }
}